package com.tcenter.beans;

import com.tcenter.dao.entities.Student;
import com.tcenter.services.StudentService;
import org.primefaces.context.RequestContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.util.Date;
import java.util.List;

/**
 * Student: Andrey Pautov
 * Date: 29.05.13
 */
@Component
@Scope("request")
public class StudentBean {

    private Student newStudent;
    private Student selectedStudent;
    private List<Student> students;

    @Autowired
    private StudentService studentService;


    public boolean isLogged() {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String email = auth.getName();
        boolean isLogged = ((email != null) && (!email.equalsIgnoreCase("anonymousUser")));
        if (isLogged) {
            selectedStudent = studentService.getByEmail(email);
            System.out.println(selectedStudent.getFirstName());

        }
        return isLogged;
    }

    public void saveNewStudent(Student student) {
        studentService.save(student);
    }


    public Student getNewStudent() {
        return newStudent;
    }

    public void setFirstName(String firsName) {
        newStudent.setFirstName(firsName);
    }

    public void setSecondName(String secondname) {
        newStudent.setSecondName(secondname);
    }

    public void setEmail(String email) {
        newStudent.setEmail(email);
    }

    public void setMobile(String mobile) {
        newStudent.setMobile(mobile);
    }

    public void setPassword(String password) {
        newStudent.setPassword(password);
    }

    public String getFirstName() {
        return selectedStudent.getFirstName();
    }

    public String getSecondName() {
        return selectedStudent.getSecondName();
    }

    public String getEmail() {
        return selectedStudent.getEmail();
    }

    public String getMobile() {
        return selectedStudent.getMobile();
    }

    public String getHighSchoolName() {
        return selectedStudent.getHighSchoolName();
    }

    public String getDepartment() {
        return selectedStudent.getDepartment();
    }

    public Byte getHighSchoolYear() {
        return selectedStudent.getHighSchoolYear();
    }

    public Date getBirthdayDate() {
        return selectedStudent.getBirthdayDate();
    }

    public void setBlocked(boolean blocked) {
        this.newStudent.setBlocked(blocked);
    }

    public boolean getIsBlocked() {
        return newStudent.isBlocked();
    }

    public String getPassword() {
        return newStudent.getPassword();
    }



    public List<Student> getAllStudents() {
        return studentService.getAll();
    }

    public void saveEditedStudent(Student editedStudent) {
        RequestContext context = RequestContext.getCurrentInstance();
        Student student = studentService.getByEmail(selectedStudent.getEmail());

        System.out.println(student.getFirstName() + " " + student.getSecondName());

        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO,
                "Student has been edited successfully", selectedStudent.getEmail());

        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

}
